package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.logiline.Logiline;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.logiline.ILogilineService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 物流运作区域信息Controller
 * Created by weih on 2016-10-14.
 */
@Controller
@RequestMapping("/mdm/logiline")
public class LogilineController extends BaseController {

    @Autowired
    ILogilineService logilineService;
    /**
     * 跳转新增物流运作区域页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/logiline/add";
    }
    /**
     * 新增物流运作区域信息
     * @param logiline 物流运作区域对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addLogiline(@RequestBody Logiline logiline,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logiline.setCreateUser(getSessionUser().getUserId());
        logiline.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logilineService.addLogiline(logiline);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑物流运作区域页面
     * @param logilineCode  物流运作区域代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String logilineCode,Model model){
        model.addAttribute("logiline", logilineService.getLogilineByCode(logilineCode));
        return "mdm/logiline/edit";
    }

    /**
     * 编辑物流运作区域信息
     * @param logiline
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editLogiline(@RequestBody Logiline logiline,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Logiline oldLogiline=logilineService.getLogilineByCode(logiline.getLogilineCode());
        oldLogiline.setId(logiline.getId());
        oldLogiline.setLogisticsAreaId(logiline.getLogisticsAreaId());
        oldLogiline.setLogisticsAreaCode(logiline.getLogisticsAreaCode());
        oldLogiline.setLogisticsAreaLineLevel(logiline.getLogisticsAreaLineLevel());
        oldLogiline.setLogisticsPointId(logiline.getLogisticsPointId());
        oldLogiline.setLogisticsPointCode(logiline.getLogisticsPointCode());
        oldLogiline.setCountryCode(logiline.getCountryCode());
        oldLogiline.setProvinceCode(logiline.getProvinceCode());
        oldLogiline.setCityCode(logiline.getCityCode());
        oldLogiline.setCountyCode(logiline.getCountyCode());
        oldLogiline.setUpdateUser(getSessionUser().getUserId());
        oldLogiline.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logilineService.updateLogiline(oldLogiline);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除物流运作区域信息
     * @param logiline 物流运作区域对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteLogiline(@RequestBody Logiline logiline,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        logiline.setUpdateUser(getSessionUser().getUserId());
        logiline.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            logilineService.deleteLogiline(logiline);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/logiline/list";
    }

    /**
     * 分页查询数据
     * @param logiline 物流运作区域对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Logiline> getLogilineData(Logiline logiline ,DataTableRequest dtrequest){
        int cnt = (int)logilineService.getLogilineCount(logiline);
        List<Logiline> list = logilineService.queryLogilines(logiline, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Logiline> result = new PaginationResult<Logiline>(list, pi);
        return new DataTableResponse<Logiline>(dtrequest.getsEcho(),result);
    }

}
